<?php

//include pdf class
include ('pdf/class.ezpdf.php'); 

//extend db model voor db functions
class PDFWriter extends dbModel {

	//set default table
	protected $sSource = "ratings";
	
	//function for getting an rating id from an student and module code
	//call this to create the rating PDF
	public function getPDF($student_code, $module_code) {
		
		//need this table
		$sSource = "ratings";
		
		//sql query for selection
		$sSql = "SELECT 
					*
				FROM 
					ratings r
				WHERE 
					r.student_code = '$student_code'
				AND
					r.module_code = '$module_code'
				";
		
		$this->trashcan();
		
		//fake the dbModel function
		$this->run_query($sSql);
		$this->oBench->start_mark();
		$this->iTotal = mysql_num_rows($this->rResult);
		$this->oBench->add_mark('counted rows');
		
		//save the rating id
		$rating_id = $this[0]['rating_id'];
		
		//call function for writing actual PDF document
		$returnPDF = $this->writePDF($rating_id);
		return $returnPDF;
	}
	
	//function for writing a PDF evaluation document
	private function writePDF($rating_id = 0) {
		
		//need this table
		$sSource = "ratings";
		
		//select query
		$sSql = "SELECT 
					*
				FROM 
					ratings r
				WHERE 
					r.rating_id = $rating_id";
		
		$this->trashcan();
		
		//fake de dbModel functie
		$this->run_query($sSql);
		$this->oBench->start_mark();
		$this->iTotal = mysql_num_rows($this->rResult);
		$this->oBench->add_mark('counted rows');
		
		if ($this->iTotal == 0) {
			return;
		}
		
		//save needed variables before overwriting the DB selection
		$studentNo = $this[0]['student_code'];
		$moduleCode = $this[0]['module_code'];
		$finalMark = $this[0]['mark'];
		
		//select new table
		$sSource = "students";
		
		//SQL query for getting studentname
		$sSql = "SELECT 
					s.student_name 
				FROM 
					students s
				WHERE 
					s.student_code = $studentNo";
		
		$this->trashcan();
		
		//fake de dbModel functie
		$this->run_query($sSql);
		$this->oBench->start_mark();
		$this->iTotal = mysql_num_rows($this->rResult);
		$this->oBench->add_mark('counted rows');
		
		//save the student name
		$studentName = $this[0]['student_name'];

		//select new table for getting the needed criteria data
		$sSource = "connect_criteria_rating";
		$sSql = "SELECT 
					c.criteria_name, ccr.comment, ccr.mark
				FROM 
					connect_criteria_rating ccr, criterias c
				WHERE 
					ccr.rating_id = $rating_id
				AND
					c.criteria_id = ccr.criteria_id
				";
		
		$this->trashcan();
		
		//fake de dbModel functie
		$this->run_query($sSql);
		$this->oBench->start_mark();
		$this->iTotal = mysql_num_rows($this->rResult);
		$this->oBench->add_mark('counted rows');

		//start a new PDF document
		$pdf = new Cezpdf(array(0,0,598,842));
		//select used font from library
		$pdf->selectFont('./pdf/fonts/Helvetica.afm');
		
		//dump results to make them accessible
		$this->dump();
		
		//add two lines at the bottom of the table with the final mark
		$this->aCache[] = array('criteria_name'=>'','comment'=>'','mark'=>'');
		$this->aCache[] = array('criteria_name'=>'','comment'=>'<b>Eindcijfer:</b>','mark'=>'<b>'.$finalMark.'</b>');
		
		//draw rating info
		$pdf->ezText('<b>Beoordeling </b>'.$moduleCode."\n".'<b>Naam: </b>'.$studentName."\n".'<b>Studentnummer: </b>'.$studentNo,14,array('justification' => 'left'));
		//set a new margin
		$pdf->ezSetDy(-50);
		//draw the table title
		$pdf->ezText('<b>Beoordeling eindopdracht:</b>',14,array('justification' => 'left'));
		//more margin
		$pdf->ezSetDy(-20);
		//draw the score table
		$pdf->ezTable($this->aCache,array('criteria_name'=>'Criteria:','comment'=>'Commentaar:','mark'=>'Deelcijfer:'),'', array('fontSize'=>'12', 'xPos'=>'left', 'width'=>'545', 'xOrientation'=>'right'));
	
		//query for the competentions
		$sSql = "SELECT 
					c.competentie_naam, c.competentie_omschrijving, crc.got_level
				FROM 
					competenties c, connect_rating_competentie crc
				WHERE 
					crc.rating_id = $rating_id
				AND
					c.id = crc.competentie_id
				";
		
		$this->trashcan();
		
		//fake de dbModel functie
		$this->run_query($sSql);
		$this->oBench->start_mark();
		$this->iTotal = mysql_num_rows($this->rResult);
		$this->oBench->add_mark('counted rows');
		
		//dump results  to make them accessible
		$this->dump();
		//some spacing
		$pdf->ezSetDy(-50);
		//draw the table title
		$pdf->ezText('<b>Behaalde competenties:</b>',14,array('justification' => 'left'));
		//more spacing
		$pdf->ezSetDy(-20);
		//draw the table with competentions
		$pdf->ezTable($this->aCache,array('competentie_naam'=>'Naam:','competentie_omschrijving'=>'Omschrijving:','got_level'=>'Niveau:'),'', array('fontSize'=>'12', 'xPos'=>'left', 'width'=>'545', 'xOrientation'=>'right'));
		//return the created PDF
		return $pdf->ezOutput();
	
	}
}

?>